package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 查询所有菜单用于回显编辑
     * @return
     */
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        /**
         * [{
         *                 id: 1,
         *                 label: '一级 1',
         *                 children: [{
         *                     id: 2,
         *                     label: '二级 1-1'
         *                 },{
         *                     id: 3,
         *                     label: '二级 1-2'
         *                 }]
         *             }]
         */
        /*
        List<Map<String,Object>> menuData = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("label","系统设置");

        List<Map<String,Object>> children = new ArrayList<>();
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id",2);
        map2.put("label","菜单管理");

        Map<String,Object> map3 = new HashMap<>();
        map3.put("id",3);
        map3.put("label","权限管理");

        children.add(map2);
        children.add(map3);

        map.put("children",children);
        menuData.add(map);
         */
        try{
            List<Map<String,Object>> menuData = menuService.findAllMenu();
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuData);
        }catch (Exception e){
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("/findAllMenuByRoleId")
    public Result findAllMenuByRoleId(Integer roleId){
        try {
            List<Integer> list =  menuService.findAllMenuByRoleId(roleId);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,list);
        }catch (Exception e){
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 根据role_id查询所有菜单权限，并且排序
     *
     * @param id
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllMenuByRole")
    public PageResult findAllPmsByRole(Integer id, @RequestBody QueryPageBean queryPageBean) {
        return menuService.findAllMenByRole(id, queryPageBean);
    }

    /**
     * 用于角色页面的数据回显分页查询
     */
    @RequestMapping("/queryPage")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.queryPage(queryPageBean);
    }

    /**
     * 根据用户id获取权限url
     */
    @RequestMapping("/findByUsername")
    public Result findByUsername(String username) {
        List<Map<String, Object>> menuList = menuService.findByUsername(username);
        return new Result(true, "菜单栏初始化成功", menuList);
    }

}